import Vue from 'vue'
import App from './App.vue'
import cookies from 'vue-cookies'
import { Toast } from 'vant'
import { LocalDomain, loginUrl } from './utils/url.js'
import utils from './utils/utils.js' // 导入全局公用方法
import uni from './utils/uni.webview.1.5.2.js' // 导入uni
import { getInitWebInfo } from './api/web'
import { RegisterByWeixin } from './api/authorize'


// 判断班赞旧域名
const initHref = function () {
  let _turnUrl = window.location.href
  if (_turnUrl.indexOf('banzan.hsy884.com') >= 0) {
    _turnUrl = _turnUrl.replace('banzan.hsy884.com', 'www.ibanbei.com')
  }
  window.location.replace(_turnUrl)
}
const GetInitWebInfo = function (params) {
  getInitWebInfo(params).then(res => {
    if (res) {
      console.log(res)
      Vue.prototype.$webInfo = res.Data
      Vue.prototype.$IntegralAlias = res.Data.IntegralAlias ? res.Data.IntegralAlias : '积分'
      Vue.prototype.$MoneyAlias = res.Data.MoneyAlias ? res.Data.MoneyAlias : '返点'
      Vue.prototype.$VitalityAlias = res.Data.VitalityAlias ? res.Data.VitalityAlias : '分红权'
      Vue.prototype.$WeikeRechargeRewardName = res.Data.WeikeRechargeRewardName ? res.Data.WeikeRechargeRewardName : '微客充值奖'
      const _isWechat = utils.wechat().isWechat
      const _isZFB = utils.isZFB()
      const _isMinProgram = window.__wxjs_environment === 'miniprogram' && wx.miniProgram
      if (res.Data && res.Data.LoginState === 1 && (_isWechat || _isMinProgram) && res.Data.WxAccountId) {
        // 微信跳转自动登录
        cookies.set('back_url', window.location.href) // 记录登录返回页面
        if (res.Data.Token) {
          cookies.set('AccessToken', res.Data.Token)
        }
        if (res.Data.WeixinAuthorizeUrl) {
          window.location.href = res.Data.WeixinAuthorizeUrl
          return
        } else {
          Toast.fail('error')
        }
      } else if (res.Data && res.Data.LoginState === 1 && (_isZFB) && res.Data.WxAccountId) {
        // 支付宝跳转自动登录
        cookies.set('back_url', window.location.href) // 记录登录返回页面
        if (res.Data.Token) {
          cookies.set('AccessToken', res.Data.Token)
        }
        if (res.Data.AliPayAuthorizeUrl) {
          window.location.href = res.Data.AliPayAuthorizeUrl
          return
        } else {
          Toast.fail('error')
        }
      } else {
        utils.changeTitle(res.Data.BusinessName)
        Vue.prototype.$webInfo = res.Data
        Vue.prototype.$IntegralAlias = res.Data.IntegralAlias ? res.Data.IntegralAlias : '积分'
        Vue.prototype.$WeixinAuthorizeUrl = res.Data.WeixinAuthorizeUrl
        window.document.documentElement.setAttribute('data-theme', res.Data.StyleSkin)
        // addVersion(res.Data.Ver)
        setFirstPage()

        window.EventBus = new Vue() // 非父子组件通信的事件总线
        new Vue({
          router,
          store,
          render: h => h(App)
        }).$mount('#app')
      }
    }
  })
}
const registerByWeixin = function (authorize, func) {
  RegisterByWeixin(authorize).then(res => {
    if (res) {
      if (res.Data) {
        cookies.set('AccessToken', res.Data)
      }
      if (typeof (func) === 'function') {
        func()
      }
    } else {
      utils.gotoPage(loginUrl)
    }
  })
}
// 获取网站基本配置
const getWebInfo = function () {
  const params = {}
  const authorize = {}
  params.domain = LocalDomain.split('//')[1]
  // params.domain = 'lftest.hsy884.com'
  params.redirect = window.location.href
  const _isWechat = utils.wechat().isWechat
  const _isZFB = utils.isZFB()
  const _isMinProgram = window.__wxjs_environment === 'miniprogram' && wx.miniProgram
  if (_isWechat || _isMinProgram) {
    params.platform = 'WX'
  } else if (_isZFB) {
    params.platform = 'APPALIPAY'
  }
  const _urlList = window.location.href.split('?')
  let query = ''
  if (_urlList && _urlList.length > 1) {
    query = _urlList[_urlList.length - 1]
  }
  if (query) {
    const arr = query.split('&')
    if (arr.length > 0) {

      for (let i = 0; i < arr.length; i++) {
        if (arr[i].indexOf('invit_user=') !== -1) {
          params.invitUser = arr[i].split('invit_user=')[1]
        } else if (arr[i].indexOf('open_id') !== -1) {
          authorize.open_id = arr[i].split('open_id=')[1]
        } else if (arr[i].indexOf('union_id') !== -1) {
          authorize.union_id = arr[i].split('union_id=')[1]
        } else if (arr[i].indexOf('nick_name') !== -1) {
          authorize.nick_name = arr[i].split('nick_name=')[1]
        } else if (arr[i].indexOf('country') !== -1) {
          authorize.country = arr[i].split('country=')[1]
        } else if (arr[i].indexOf('province') !== -1) {
          authorize.province = arr[i].split('province=')[1]
        } else if (arr[i].indexOf('city') !== -1) {
          authorize.city = arr[i].split('city=')[1]
        } else if (arr[i].indexOf('sex') !== -1) {
          authorize.sex = arr[i].split('sex=')[1]
        } else if (arr[i].indexOf('face') !== -1) {
          authorize.face = arr[i].split('face=')[1]
        } else if (arr[i].indexOf('platform') !== -1) {
          authorize.platform = arr[i].split('platform=')[1]
        } else if (arr[i].indexOf('invit_user_id') !== -1) {
					authorize.invit_user_flag = arr[i].split('invit_user_id=')[1]
				}
      }
    }
  }
  if (authorize.open_id || authorize.union_id) {
		registerByWeixin(authorize, GetInitWebInfo)
  } else {
    GetInitWebInfo(params)
  }
}

// 设置后退键首页
const setFirstPage = function () {
  if (!sessionStorage.getItem('firstPage')) {
    console.log(window.location.href)
    let firstPage = window.location.href
    if (firstPage.indexOf('#') >= 0) {
      firstPage = firstPage.split('#')[1]
      if (firstPage && firstPage.indexOf('?') >= 0) {
        firstPage = firstPage.split('?')[0]
      }
    }
    sessionStorage.setItem('firstPage', firstPage)
  }
}
initHref()

Vue.prototype.$webInfo = {}
Vue.prototype.$IntegralAlias = ''
getWebInfo()

import './vant'
import router from './router'
import store from './store'

Vue.config.productionTip = false
Vue.prototype.$remUnit = 75 // 设置全局rem基数,在某些场景需要用到，比如粘性布局时计算窗口宽度不同，粘住时距离顶部的距离也不同

import '@/styles/app.scss' // 全局样式
import 'components/index.scss' // 所有自定义组件样式
import './assets/css/animate.css' // animate动画
import './icons' // icon
import './utils/amap' // 引入高德地图相关
import conf from './utils/baseConfig.js' // 导入全局公共配置
Vue.prototype.$utils = utils
Vue.prototype.$uni = uni
Vue.prototype.$conf = conf

//高德地图
import VueAMap from 'vue-amap';
Vue.use(VueAMap)
// 初始化vue-amap
VueAMap.initAMapApiLoader({
    key: '在高德地图申请的key',
    // 插件集合 （插件按需引入）
    plugin: [
        "AMap.Autocomplete", //输入提示插件
        "AMap.PlaceSearch", //POI搜索插件
        "AMap.Scale", //右下角缩略图插件 比例尺
        "AMap.OverView", //地图鹰眼插件
        "AMap.ToolBar", //地图工具条
        "AMap.MapType", //类别切换控件，实现默认图层与卫星图、实施交通图层之间切换的控制
        "AMap.PolyEditor", //编辑 折线多，边形
        "AMap.CircleEditor", //圆形编辑器插件
        "AMap.Geolocation" //定位控件，用来获取和展示用户主机所在的经纬度位置
    ]
});

